package com.pc.scrs.dao;

import static com.pc.scrs.dao.TeacherDynamicSqlSupport.*;
import static org.mybatis.dynamic.sql.SqlBuilder.*;

import com.pc.scrs.bean.po.Teacher;
import java.util.List;
import java.util.Optional;
import javax.annotation.Generated;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.SqlBuilder;
import org.mybatis.dynamic.sql.delete.DeleteDSLCompleter;
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
import org.mybatis.dynamic.sql.render.RenderingStrategy;
import org.mybatis.dynamic.sql.select.CountDSLCompleter;
import org.mybatis.dynamic.sql.select.SelectDSLCompleter;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.mybatis.dynamic.sql.update.UpdateDSL;
import org.mybatis.dynamic.sql.update.UpdateDSLCompleter;
import org.mybatis.dynamic.sql.update.UpdateModel;
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils;

@Mapper
public interface TeacherDao {
    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    BasicColumn[] selectList = BasicColumn.columnList(id, mobile, email, password, name, intro, avatar, status, createTime, updateTime, enable, userId);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @SelectProvider(type=SqlProviderAdapter.class, method="select")
    long count(SelectStatementProvider selectStatement);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @DeleteProvider(type=SqlProviderAdapter.class, method="delete")
    int delete(DeleteStatementProvider deleteStatement);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @InsertProvider(type=SqlProviderAdapter.class, method="insert")
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="record.id", before=false, resultType=Integer.class)
    int insert(InsertStatementProvider<Teacher> insertStatement);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @SelectProvider(type=SqlProviderAdapter.class, method="select")
    @ResultMap("TeacherResult")
    Optional<Teacher> selectOne(SelectStatementProvider selectStatement);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @SelectProvider(type=SqlProviderAdapter.class, method="select")
    @Results(id="TeacherResult", value = {
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="mobile", property="mobile", jdbcType=JdbcType.VARCHAR),
        @Result(column="email", property="email", jdbcType=JdbcType.VARCHAR),
        @Result(column="password", property="password", jdbcType=JdbcType.VARCHAR),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="intro", property="intro", jdbcType=JdbcType.VARCHAR),
        @Result(column="avatar", property="avatar", jdbcType=JdbcType.VARCHAR),
        @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_time", property="updateTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="enable", property="enable", jdbcType=JdbcType.INTEGER),
        @Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER)
    })
    List<Teacher> selectMany(SelectStatementProvider selectStatement);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @UpdateProvider(type=SqlProviderAdapter.class, method="update")
    int update(UpdateStatementProvider updateStatement);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default long count(CountDSLCompleter completer) {
        return MyBatis3Utils.countFrom(this::count, teacher, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int delete(DeleteDSLCompleter completer) {
        return MyBatis3Utils.deleteFrom(this::delete, teacher, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int deleteByPrimaryKey(Integer id_) {
        return delete(c ->
            c.where(id, isEqualTo(id_))
        );
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int insert(Teacher record) {
        return MyBatis3Utils.insert(this::insert, record, teacher, c ->
            c.map(mobile).toProperty("mobile")
            .map(email).toProperty("email")
            .map(password).toProperty("password")
            .map(name).toProperty("name")
            .map(intro).toProperty("intro")
            .map(avatar).toProperty("avatar")
            .map(status).toProperty("status")
            .map(createTime).toProperty("createTime")
            .map(updateTime).toProperty("updateTime")
            .map(enable).toProperty("enable")
            .map(userId).toProperty("userId")
        );
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int insertSelective(Teacher record) {
        return MyBatis3Utils.insert(this::insert, record, teacher, c ->
            c.map(mobile).toPropertyWhenPresent("mobile", record::getMobile)
            .map(email).toPropertyWhenPresent("email", record::getEmail)
            .map(password).toPropertyWhenPresent("password", record::getPassword)
            .map(name).toPropertyWhenPresent("name", record::getName)
            .map(intro).toPropertyWhenPresent("intro", record::getIntro)
            .map(avatar).toPropertyWhenPresent("avatar", record::getAvatar)
            .map(status).toPropertyWhenPresent("status", record::getStatus)
            .map(createTime).toPropertyWhenPresent("createTime", record::getCreateTime)
            .map(updateTime).toPropertyWhenPresent("updateTime", record::getUpdateTime)
            .map(enable).toPropertyWhenPresent("enable", record::getEnable)
            .map(userId).toPropertyWhenPresent("userId", record::getUserId)
        );
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default Optional<Teacher> selectOne(SelectDSLCompleter completer) {
        return MyBatis3Utils.selectOne(this::selectOne, selectList, teacher, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default List<Teacher> select(SelectDSLCompleter completer) {
        return MyBatis3Utils.selectList(this::selectMany, selectList, teacher, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default List<Teacher> selectDistinct(SelectDSLCompleter completer) {
        return MyBatis3Utils.selectDistinct(this::selectMany, selectList, teacher, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default Optional<Teacher> selectByPrimaryKey(Integer id_) {
        return selectOne(c ->
            c.where(id, isEqualTo(id_))
        );
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int update(UpdateDSLCompleter completer) {
        return MyBatis3Utils.update(this::update, teacher, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    static UpdateDSL<UpdateModel> updateAllColumns(Teacher record, UpdateDSL<UpdateModel> dsl) {
        return dsl.set(mobile).equalTo(record::getMobile)
                .set(email).equalTo(record::getEmail)
                .set(password).equalTo(record::getPassword)
                .set(name).equalTo(record::getName)
                .set(intro).equalTo(record::getIntro)
                .set(avatar).equalTo(record::getAvatar)
                .set(status).equalTo(record::getStatus)
                .set(createTime).equalTo(record::getCreateTime)
                .set(updateTime).equalTo(record::getUpdateTime)
                .set(enable).equalTo(record::getEnable)
                .set(userId).equalTo(record::getUserId);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    static UpdateDSL<UpdateModel> updateSelectiveColumns(Teacher record, UpdateDSL<UpdateModel> dsl) {
        return dsl.set(mobile).equalToWhenPresent(record::getMobile)
                .set(email).equalToWhenPresent(record::getEmail)
                .set(password).equalToWhenPresent(record::getPassword)
                .set(name).equalToWhenPresent(record::getName)
                .set(intro).equalToWhenPresent(record::getIntro)
                .set(avatar).equalToWhenPresent(record::getAvatar)
                .set(status).equalToWhenPresent(record::getStatus)
                .set(createTime).equalToWhenPresent(record::getCreateTime)
                .set(updateTime).equalToWhenPresent(record::getUpdateTime)
                .set(enable).equalToWhenPresent(record::getEnable)
                .set(userId).equalToWhenPresent(record::getUserId);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int updateByPrimaryKey(Teacher record) {
        return update(c ->
            c.set(mobile).equalTo(record::getMobile)
            .set(email).equalTo(record::getEmail)
            .set(password).equalTo(record::getPassword)
            .set(name).equalTo(record::getName)
            .set(intro).equalTo(record::getIntro)
            .set(avatar).equalTo(record::getAvatar)
            .set(status).equalTo(record::getStatus)
            .set(createTime).equalTo(record::getCreateTime)
            .set(updateTime).equalTo(record::getUpdateTime)
            .set(enable).equalTo(record::getEnable)
            .set(userId).equalTo(record::getUserId)
            .where(id, isEqualTo(record::getId))
        );
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int updateByPrimaryKeySelective(Teacher record) {
        return update(c ->
            c.set(mobile).equalToWhenPresent(record::getMobile)
            .set(email).equalToWhenPresent(record::getEmail)
            .set(password).equalToWhenPresent(record::getPassword)
            .set(name).equalToWhenPresent(record::getName)
            .set(intro).equalToWhenPresent(record::getIntro)
            .set(avatar).equalToWhenPresent(record::getAvatar)
            .set(status).equalToWhenPresent(record::getStatus)
            .set(createTime).equalToWhenPresent(record::getCreateTime)
            .set(updateTime).equalToWhenPresent(record::getUpdateTime)
            .set(enable).equalToWhenPresent(record::getEnable)
            .set(userId).equalToWhenPresent(record::getUserId)
            .where(id, isEqualTo(record::getId))
        );
    }

    @Select("select mobile from edu_teacher")
    List<String> queryMobileList();

    @Select("select email from edu_teacher")
    List<String> queryEmailList();

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @SelectProvider(type = SqlProviderAdapter.class, method = "select")
    public List<Teacher> getTeacherList(SelectStatementProvider provider);

    default List<Teacher> queryTeacherList(String mobile, String email, Integer enable){
        String finalMobile = null;
        if (mobile != null && !"".equals(mobile)) {
            finalMobile = "%" + mobile + "%";
        }
        String finalEmail = null;
        if (email != null && !"".equals(email)) {
            finalEmail = "%" + email + "%";
        }
        if ("".equals(enable)) {
            enable = null;
        }

        SelectStatementProvider provider = SqlBuilder.select(
                id,
                TeacherDynamicSqlSupport.mobile,
                TeacherDynamicSqlSupport.email,
                name,
                TeacherDynamicSqlSupport.enable,
                createTime
        ).from(teacher)
                .where(TeacherDynamicSqlSupport.mobile,isLikeWhenPresent(finalMobile))
                .and(TeacherDynamicSqlSupport.email, isLikeWhenPresent(finalEmail))
                .and(TeacherDynamicSqlSupport.enable, isEqualToWhenPresent(enable))
                .build().render(RenderingStrategy.MYBATIS3);
        return this.getTeacherList(provider);
    }

    @Update("update edu_teacher set enable = 0 where id = #{id}")
    Integer disableTeacher(Integer id);

    @Update("update edu_teacher set enable = 1 where id = #{id}")
    Integer enableTeacher(Integer id);

    @Update("update edu_teacher set password = #{password} where id = #{id}")
    Integer updatePassword(@Param("id") Integer id,@Param("password") String password);

    @Select("select id,name,mobile,email,avatar,status,intro from edu_teacher where id = #{id}")
    Teacher queryTeacherById(Integer id);
}
